package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.entity.MemberTransfer;
import com.dingreading.cloud.soms.entity.table.MemberTransferTableDef;
import com.dingreading.cloud.soms.mapper.MemberTransferMapper;
import com.dingreading.cloud.soms.service.MemberTransferService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-02-24
 */
@Service
public class MemberTransferServiceImpl extends BaseServiceImpl<MemberTransferMapper, MemberTransfer, MemberTransferTableDef> implements MemberTransferService {

    @Override
    protected MemberTransferTableDef getTable() {
        return MemberTransferTableDef.memberTransfer;
    }

    @Override
    public Page<MemberTransfer> pageList(PageUtil pageUtil, String corpUid, String fromStoreUid, String toStoreUid, String keywords, Integer status) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid));
        if (StringUtils.isNotBlank(fromStoreUid))
            condition.and(table.fromStoreUid.eq(fromStoreUid));
        if (StringUtils.isNotBlank(toStoreUid))
            condition.and(table.toStoreUid.eq(toStoreUid));
        if (StringUtils.isNotBlank(keywords))
            condition.and(table.memberName.like(keywords)
                    .or(table.mob.like(keywords))
            );
        if (status != null)
            condition.and(table.status.eq(status));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public int dropBean(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.storeUid.eq(storeUid))
                .and(table.status.eq(0));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public MemberTransfer getByStoreUidAndUid(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.storeUid.eq(storeUid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public MemberTransfer getByToStoreUidAndUid(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.toStoreUid.eq(storeUid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public boolean updateStatus(String storeUid, String uid, Integer status, String staffUid, String staffName, String checkRemark) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.toStoreUid.eq(storeUid))
                .and(table.status.eq(0));

        return UpdateChain.of(MemberTransfer.class)
                .set(table.checkRemark, checkRemark)
                .set(table.status, status)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public boolean haveMember(String storeUid, String memberUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.memberUid.eq(memberUid))
                .and(table.fromStoreUid.eq(storeUid))
                .and(table.status.eq(0));
        return exists(condition);
    }
}
